<!DOCTYPE html>
<title>Service Worker: clients.openWindow() tests (using testRunner)</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.js"></script>
<script>
// This test is using testRunner to grant itself the notification permission and
// to simulate a click on a notification. A couple of changes would allow it to
// be run as a manual test by other browser vendors.
if (window.testRunner)
    testRunner.setPermission('notifications', 'granted', location.origin, location.origin);

var t = async_test('clients.openWindow() behaved as expected');
t.step(function() {
    var scope = 'resources/blank.html'
    service_worker_unregister_and_register(
        t, 'resources/clients-openwindow.js', scope)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() { return with_iframe(scope); })
      .then(function(frame) {
          var w = frame.contentWindow;
          w.navigator.serviceWorker.onmessage = t.step_func(onMessage);
          w.navigator.serviceWorker.controller.postMessage('start');
        })
      .catch(unreached_rejection(t));

    var result = [];
    var expected = ['openWindow() can\'t open a window without a user interaction',
                    'openWindow() error is InvalidAccessError',
                    'openWindow() can open cross origin windows',
                    'openWindow() result: null',
                    'openWindow() can open not controlled windows',
                    'openWindow() result: [object WindowClient]',
                    'openWindow() can open controlled windows',
                    'openWindow() result: [object WindowClient]',
                    ' url: ' + location.origin + '/serviceworker/resources/blank.html',
                    ' visibilityState: visible',
                    ' focused: false',
                    ' frameType: top-level',
                    'openWindow() can open about:blank',
                    'openWindow() result: null',
                    'openWindow() can open about:crash',
                    'openWindow() result: null',
                    'openWindow() can not open an invalid url',
                    'openWindow() error is: TypeError',
                    'openWindow() can not open view-source scheme',
                    'openWindow() error is: TypeError',
                    'openWindow() can not open file scheme',
                    'openWindow() error is: TypeError',
                    ];

    function onMessage(e) {
      var message = e.data;

      if (typeof(message) === 'object') {
        if (message.type !== 'click')
          return;
        if (window.testRunner)
          testRunner.simulateWebNotificationClick(message.title);
        return;
      }

      if (message === 'quit') {
        assert_array_equals(result, expected,
                            'Worker should post back expected messages.');
        service_worker_unregister_and_done(t, scope);
      } else {
        result.push(message);
      }
    }
  });
</script>
